package ru.msu.cmc.sp.oberon2;
import org.antlr.runtime.tree.CommonTree;
import org.antlr.runtime.tree.ParseTree;
import org.antlr.works.interpreter.EditorInterpreterTreeModel;
import org.antlr.works.utils.awtree.AWTreePanel;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.tree.*;

public class MyFrame extends JFrame {
	
	private static final long serialVersionUID = 1L;
	static final int WIDTH = 500;
  	static final int HEIGHT = 600;

  	public static void convert(CommonTree tree, ParseTree pt) {
		if (tree.getChildren() == null) {
			return;
		}
		for (int i = 0; i < tree.getChildren().size(); ++i) {
	      	Object ob = tree.getChildren().get(i);
	      	ParseTree pt1 = new ParseTree(((CommonTree)ob).token);
	      	//ParseTree pt1 = new ParseTree(((InterAST)ob).scope);
      		convert((CommonTree)ob, pt1);
	      	pt.addChild(pt1);
		}
  	}
  
  
	public MyFrame(CommonTree r) {
		EditorInterpreterTreeModel treeModel = null;
		treeModel = new EditorInterpreterTreeModel();
		AWTreePanel awTreePanel = null;
		awTreePanel = new AWTreePanel(treeModel);
		ParseTree pt = new ParseTree(r.token);
		convert(r, pt);
		treeModel.setTree(pt);
	    awTreePanel.setRoot((TreeNode)treeModel.getRoot());
	    awTreePanel.refresh();
	    
		Container content = getContentPane();
		content.add(awTreePanel, BorderLayout.CENTER);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				Frame f = (Frame) e.getSource();
				f.setVisible(false);
				f.dispose();
				// System.exit(0);
			}
		});
		setSize(WIDTH, HEIGHT);
		setVisible(true);
	}  
}